package api

import (
	"github.com/gin-gonic/gin"
	"guard/internal/app/user/serivce"
	"guard/internal/app/user/store"
	"guard/internal/db"
	"guard/internal/middleware"
)

var userService serivce.UserService

/**
这一个模块，或者说是组件，只是作为试更新，下一个版本，会将oauth也单独提取出来作为一个组件，或者模块。这样app文件夹整体就显得更容易理解
*/

func init() {
	userService = serivce.NewUserService(store.NewUserStore(db.NewMySqlStore()))
}

func InitUserRouter(router *gin.Engine) {

	// 登录，注销
	router.GET("login", login)
	router.POST("login", middleware.ErrHtmlHandler("login.html"), login)
	router.GET("logout", middleware.AuthLogin, middleware.ErrHtmlHandler("error.html"), logout)
	// openid
	router.POST("userinfo", middleware.ErrJsonHandler, userInfo)

	// 首页属于用户信息展示页面，在user模块里
	router.GET("index.html", middleware.AuthLogin, middleware.ErrHtmlHandler("error.html"), indexUser)

	// 注册用户
	router.POST("register", middleware.ErrJsonHandler, registerUser)
	// 修改用户信息
	router.POST("modify_user_userinfo", middleware.AuthLoginJson, middleware.ErrJsonHandler, modifyUserInfo)
	// 修改用户密码
	router.POST("modify_user_password", middleware.AuthLoginJson, middleware.ErrJsonHandler, modifyUserPassword)

	// 用户管理
	user := router.Group("user", middleware.ErrJsonHandler, middleware.TokenAuth("SYS:OAUTH:ALL"))
	{
		user.GET("", userPage)
		user.GET("/:query", queryUser)
		user.POST("add", addUser)
		user.DELETE("/:username", removeUser)
	}
}
